<template>
  <div class="equipment-management">
    <div class="search-bar">
      <el-input
        v-model="searchQuery"
        placeholder="搜索"
        class="search-input"
      >
        <template #append>
          <el-button type="primary">搜索</el-button>
        </template>
      </el-input>
    </div>

    <el-table :data="equipmentList" style="width: 100%">
      <el-table-column label="器材图片" width="150">
        <template #default="scope">
          <img :src="scope.row.image" class="equipment-image" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="器材名称" width="150" />
      <el-table-column prop="code" label="器材编号" width="150" />
      <el-table-column prop="model" label="器材型号" width="150" />
      <el-table-column prop="quantity" label="数量" width="100" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button
            type="danger"
            size="small"
            @click="handleRemove(scope.row)"
          >
            下架
          </el-button>
          <el-button
            type="warning"
            size="small"
            @click="handleEdit(scope.row)"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[5, 10, 20, 50]"
        layout="prev, pager, next, sizes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const equipmentList = ref([
  {
    image: '/equipment-images/funnel.png',
    name: '漏斗',
    code: 'z-09',
    model: '6类',
    quantity: 10
  },
  {
    image: '/equipment-images/flask.png',
    name: '长颈烧杯',
    code: 'z-08',
    model: '20cm',
    quantity: 5
  },
  {
    image: '/equipment-images/microscope.png',
    name: '显微镜',
    code: 'z-12',
    model: '中型',
    quantity: 15
  },
  {
    image: '/equipment-images/tripod.png',
    name: '三脚架',
    code: 'z-05',
    model: '半径5cm',
    quantity: 2
  }
])

const handleRemove = (row) => {
  ElMessageBox.confirm(
    '确认要下架该器材吗？',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      ElMessage({
        type: 'success',
        message: '下架成功',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消下架',
      })
    })
}

const handleEdit = (row) => {
  // 实现编辑功能
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.equipment-management {
  padding: 20px;
}

.search-bar {
  margin-bottom: 20px;
}

.search-input {
  width: 300px;
}

.equipment-image {
  width: 80px;
  height: 80px;
  object-fit: contain;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>

